package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.qo.JsonResult;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.IDepartmentService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("department")
@Controller
public class DepartmentController {

	@Autowired
	private IDepartmentService departmentService;
	
	@RequestMapping("list")
	@RequiresPermissions(value = {"department:list","部门列表"},logical = Logical.OR)
	public String list(Model model, @ModelAttribute("qo")QueryObject qo){
		//int i = 1/0;
		PageInfo<Department> pageInfo = departmentService.query(qo);
		model.addAttribute("pageInfo",pageInfo);
		return "department/list";
	}
	
	/*@RequestMapping("input")
	@RequriedPermission(name="部门编辑",expression = "department:input")
	public String input(Long id,Model model){
		if (id!=null) {
			Department department = departmentService.select(id);
			model.addAttribute("department", department);
		}
		return "department/input";
	}*/
	
	@RequestMapping("delete")
	@RequiresPermissions(value = {"department:delete","部门删除"},logical = Logical.OR)
	@ResponseBody
	public JsonResult delete(Long id){
		try {
			if (id!=null) {
				departmentService.delete(id);
			}
			return new JsonResult(true,"删除成功");
		}catch (Exception e){
			e.printStackTrace();
			return new JsonResult(false,"删除失败");
		}
	}
	@RequestMapping("saveOrUpdate")
	@RequiresPermissions(value = {"department:saveOrUpdate","部门新增或更新"},logical = Logical.OR)
	@ResponseBody
	public JsonResult saveOrUpdate(Department department){
		try {
			if (department.getId()!=null) {
				departmentService.update(department);
			}else {
				departmentService.insert(department);
			}
			return new JsonResult(true,"新增或修改成功");
		}catch (Exception e){
			e.printStackTrace();
			return new JsonResult(false,"新增或修改失败");
		}

	}
}
